﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace blackhouse.Web.Controls {
	public class CompositeActionControl<ACTION_SUB_TYPE> {

		#region Fields

		private List<ActionControl> actions = new List<ActionControl>();

		#endregion

		#region Methods

		public ActionControl Register(WebControl control, string action) {
			Type t = typeof(ACTION_SUB_TYPE);
			ActionControl actionCtrl = Activator.CreateInstance(t, control) as ActionControl;
			if(actionCtrl != null) {
				actionCtrl.AddParameter("action", action);
				actions.Add(actionCtrl);
			}
			return actionCtrl;
		}

		public CompositeActionControl<ACTION_SUB_TYPE> AddParameter(string key, object value) {
			this.actions.ForEach(a => a.AddParameter(key, value));
			return this;
		}

		public void Bind() {
			this.actions.ForEach(a => a.Bind());
		}

		#endregion

		public string GetPostbackAction() {
			if(HttpContext.Current != null) {
				return HttpContext.Current.Request["action"];
			} else return "";
		}

		public bool IsPostbackAction(string action) {
			return this.GetPostbackAction() == action;
		}

	}
}